package com.geeklei.media.ui.widget;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geeklei.media.ui.IUserEventListener;
import com.geeklei.media.ui.R;
import com.geeklei.media.ui.menu.popup.BaseMenuPopup;
import com.geeklei.media.ui.menu.popup.ListPopupWindow;
import com.geeklei.media.ui.menu.popup.OnShowHideListener;
import com.geeklei.media.ui.menu.popup.PopListAdapter;
import com.geeklei.media.ui.utils.DateTimeHelper;

public class StatusBar extends RelativeLayout {
    private View mBackView;
    private BatteryController mBatteryController;
    private BatteryStatusIconView mBatteryView;
    private TextView mCurrentTimeView;
    //    private FrameLayout mCustomView;
    private OnClickListener mOnClickListener = new OnClickListener() {
        public void onClick(View view) {
            if ((view == StatusBar.this.mBackView || view.getId() == R.id.vp_top_title) && StatusBar.this.mActivity != null) {
                mActivity.onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
            } else if (view == StatusBar.this.mSpeedBtn && mIUserEventListener != null) {
                mSpeedSettingPopupAdapter = new PopListAdapter(getContext().getApplicationContext());
                mSpeedListPopup = new ListPopupWindow(getContext().getApplicationContext(), R.string.vp_speed, mSpeedSettingPopupAdapter);
                mSpeedListPopup.setSources(mIUserEventListener.getSpeedList());
                mSpeedListPopup.setOnSourceSelectListener(mIUserEventListener.getSpeedSelectListener());
                mSpeedListPopup.setCurrentSource(mIUserEventListener.getCurrentSpeed());
                mSpeedListPopup.setShowHideListener(mMenuShowHideListener);
                mSpeedListPopup.show(mAncor);
            }
        }
    };

    private PopListAdapter mSpeedSettingPopupAdapter;
    private ListPopupWindow mSpeedListPopup;
    //    private TextView mSubTitleView;
    private TextView mTitleView;
    private Activity mActivity;

    private Float mCurrentSpeed = 1.0f;
    private OnShowHideListener<BaseMenuPopup> mMenuShowHideListener;
    private Button mSpeedBtn;
    private com.geeklei.media.ui.IUserEventListener mIUserEventListener;
    private RelativeLayout mAncor;
    private RelativeLayout back_area;

    public class BatteryController extends BroadcastReceiver {
        private BatteryStatusIconView mBatteryView;
        private final Context mContext;
        private int mLevel = 0;
        private boolean mPlugged = false;

        public BatteryController(Context context) {
            this.mContext = context;
        }

        public void update(BatteryStatusIconView batteryStatusIconView) {
            this.mBatteryView = batteryStatusIconView;
            this.mContext.registerReceiver(this, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        }

        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.intent.action.BATTERY_CHANGED")) {
                boolean z = this.mPlugged;
                this.mPlugged = intent.getIntExtra("plugged", 0) != 0;
                if (z != this.mPlugged) {
                    BatteryIcon.getInstance(this.mContext).clear();
                }
                this.mLevel = intent.getIntExtra("level", 0);
                this.mBatteryView.setImageLevel(this.mLevel);
                this.mBatteryView.updateCharging(this.mPlugged);
                StatusBar.this.mBatteryController.release();
            }
        }

        public void release() {
            try {
                this.mContext.unregisterReceiver(this);
            } catch (Exception e) {
            }
        }
    }

    public StatusBar(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init();
    }

    public StatusBar(Context context) {
        super(context);
        init();
    }

    public void updateStatus(CharSequence charSequence, CharSequence charSequence2) {
        this.mTitleView.setText(charSequence);
//        this.mSubTitleView.setText(charSequence2);
    }

    private void init() {
        this.mBatteryController = new BatteryController(getContext().getApplicationContext());
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        this.back_area = (RelativeLayout) findViewById(R.id.vp_back_area);
        this.back_area.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });
        this.mTitleView = (TextView) findViewById(R.id.vp_top_title);
        this.mTitleView.setOnClickListener(mOnClickListener);
//        this.mSubTitleView = (TextView) findViewById(R.id.vp_top_sub_title);
        this.mBackView = findViewById(R.id.vp_top_back);
        this.mBackView.setOnClickListener(this.mOnClickListener);
        this.mBatteryView = (BatteryStatusIconView) findViewById(R.id.vp_battery);
        this.mCurrentTimeView = (TextView) findViewById(R.id.vp_current_time);

        this.mSpeedBtn = (Button) findViewById(R.id.btn_set_speed);
        this.mSpeedBtn.setText(mCurrentSpeed + "X");
        this.mSpeedBtn.setOnClickListener(mOnClickListener);
    }

    public void attachActivity(Activity activity) {
        this.mActivity = activity;
    }

    public void attachSpeed(com.geeklei.media.ui.IUserEventListener userEventListener, RelativeLayout mAnchor, OnShowHideListener<BaseMenuPopup> mMenuShowHideListener) {
        this.mIUserEventListener = userEventListener;
        this.mAncor = mAnchor;
        this.mMenuShowHideListener = mMenuShowHideListener;
    }

    protected void onVisibilityChanged(View view, int i) {
        super.onVisibilityChanged(view, i);
        if (view == this || view == getParent()) {
            this.mCurrentTimeView.setText(DateTimeHelper.getCurrentString("HH:mm"));
            if (i == VISIBLE) {
                this.mBatteryController.update(this.mBatteryView);
            }
        }
    }

    /**
     * 显示设置菜单
     */

    /**
     * 设置速度
     *
     * @param speed 速度值
     */
    public void updateSppedStatus(float speed) {
        mCurrentSpeed = speed;
        mSpeedBtn.setText(speed + "X");

    }
}
